package com._51cto.healthy.excetion;

import com._51cto.healthy.content.HttpStatus;
import com._51cto.healthy.content.R;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.nio.file.AccessDeniedException;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = AccessDeniedException.class)
    public R AccessDeniedExceptionHandler(Exception ex){
        //没有权限时
        System.out.println("---权限不足---");
        return R.fail()
                .setMsg("用户权限不足,禁止访问!");
    }

    @ExceptionHandler(value = IllegalArgumentException.class)
    public R illegalArgumentHandler(Exception ex) {
        System.out.println("-------参数验证异常-------");
        System.out.println(ex.getMessage());
        return R.fail()
                .setMsg(ex.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    public R exceptionHandler(Exception ex) {
        System.out.println("-------内部代码错误-------");
        ex.printStackTrace();
        return R.fail().setCode(HttpStatus.ERROR).setMsg("数据异常，请检测后重试！");
    }
}
